package com.cjm.one.common.config.jackson.converter;

import com.cjm.one.model.base.BaseEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
// 枚举序列化
@Slf4j
public class BaseEnumSerializer extends JsonSerializer<BaseEnum> {
    @Override
    public void serialize(BaseEnum baseEnum, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        if (baseEnum!= null) {
            log.info("序列化枚举:{}", baseEnum);
            log.info("序列化枚举:{}", baseEnum.getCode());
            jsonGenerator.writeString(String.valueOf(baseEnum.getCode()));

        } else {
            // 如果枚举值为 null，不进行序列化操作
            jsonGenerator.writeNull();
        }
    }
}
